#include "cmsis_os2.h"
#include "lvgl.h"
#include "SquareLineStudioUI/ui.h"


extern osMutexId_t LvglMutexHandle;


void StartLvglTask(void *argument)
{
    osMutexAcquire(LvglMutexHandle, osWaitForever);

    static lv_coord_t col_dsc[] = { 70, 70, 70, LV_GRID_TEMPLATE_LAST };
    static lv_coord_t row_dsc[] = { 50, 50, 50, LV_GRID_TEMPLATE_LAST };

    lv_obj_t* cont = lv_obj_create(lv_scr_act());                   /* 创建一个容器 */
    lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);       /* 设置网格的列数量 */
    lv_obj_set_style_grid_row_dsc_array(cont, row_dsc, 0);          /* 设置网格的行数量 */
    lv_obj_set_size(cont, 300, 220);                                /* 设置容器大小 */
    lv_obj_center(cont);                                            /* 设置位置 - 居中 */
    lv_obj_set_layout(cont, LV_LAYOUT_GRID);                        /* 开启 GRID 网格 */
    
    lv_obj_t* obj;
    obj = lv_btn_create(cont);                                      /* 创建一个 btn 子类 */
    lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0,  1,LV_GRID_ALIGN_STRETCH, 0, 1);    /* 把 btn 对象添加到网格(0,0)位置 */

    obj = lv_switch_create(cont);                                   /* 创建一个 switch 子类 */
    lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_STRETCH, 1, 1);    /* 把 switch 对象添加到网格(0,1)位置 */

    osMutexRelease(LvglMutexHandle);

    for(;;)
    {
        osMutexAcquire(LvglMutexHandle, osWaitForever);
        lv_timer_handler();
        osMutexRelease(LvglMutexHandle);
        osDelay(5);
    }
}
